package com.example.readbook.Sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.readbook.Bean.JournalBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Package    : com.example.readbook.Sql
 * ClassName  : JournalUtils
 * Description: $读后感表操作类
 * Date       : 2019/3/26 14:29
 */
public class JournalUtils {
    private static Sqlite dbhelper;

    public static JournalUtils getInstence(Context context) {
        if (dbhelper == null) {
            dbhelper = new Sqlite(context);
        }
        return new JournalUtils();
    }

    //读后感表
    private static final String journalEntry = "create table if not exists journal("
            + "id integer primary key autoincrement," + "name text," + "entry text," + "time text" + ")";

    //插入读后感
    public void addJournal(String bookName, String journal) {
        SQLiteDatabase writableDatabase = dbhelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", bookName);
        values.put("entry", journal);
        values.put("time", System.currentTimeMillis() + "");
        writableDatabase.insert("journal", null, values);
        writableDatabase.close();
    }

    //读取全部读后感
    public List<JournalBean> readAllData() {
        SQLiteDatabase readableDatabase = dbhelper.getReadableDatabase();
        Cursor cursor = readableDatabase.rawQuery("select*from journal", new String[]{});
        List<JournalBean> beans = new ArrayList<>();
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String entry = cursor.getString(cursor.getColumnIndex("entry"));
            String time = cursor.getString(cursor.getColumnIndex("time"));
            beans.add(new JournalBean(id, name, entry, time));
        }
        return beans;
    }

    //读取某本书读后感
    public List<JournalBean> readBookData(String bookName) {
        Log.e("yxs", "查询的书名：" + bookName);

        SQLiteDatabase readableDatabase = dbhelper.getReadableDatabase();
        Cursor cursor = readableDatabase.rawQuery("select*from journal where name=?", new String[]{bookName});
        List<JournalBean> beans = new ArrayList<>();
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String entry = cursor.getString(cursor.getColumnIndex("entry"));
            String time = cursor.getString(cursor.getColumnIndex("time"));
            beans.add(new JournalBean(id, name, entry, time));
        }
        cursor.close();
        readableDatabase.close();
        return beans;
    }

    //删除一个读后感
    public void delete(int id) {
        SQLiteDatabase writableDatabase = dbhelper.getWritableDatabase();
        writableDatabase.delete("journal", "id=?", new String[]{id + ""});
        writableDatabase.close();
    }

    //更新一条记录
    public void update(int id, String data) {
        SQLiteDatabase writableDatabase = dbhelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("entry", data);
        values.put("time", System.currentTimeMillis() + "");
        writableDatabase.update("journal", values, "id=?", new String[]{id + ""});
        writableDatabase.close();
    }
}
